//
//  VistorLoginView.swift
//  Weobo
//
//  Created by 史亚洲 on 16/1/11.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit


/*
1.protocol: 注意swift中没有 '@'
2.需要遵守根协议<NSObject>
3.如果不遵守NSObjectProtocol基协议,就无法使用weak关键字
4.声明可选的协议方法,需要使用optional关键字,是OC中的关键字,需要在协议前面加上'@objc'
*/
@objc protocol VistorLoginViewDelegate: NSObjectProtocol {

    //协议方法
    
    //1.用户将要注册,属于可选的
    optional func userWillRegister()
    
    //2.用户将要登录
    func userWillLogin()



}


class VistorLoginView: UIView {
    
    //声明代理.swift属性,默认属性关键字是strong,声明弱引用的属性,需要使用weak
    weak var vistorViewDelegate: VistorLoginViewDelegate?
    
    //MARK按钮的监听事件方法
    @objc private func loginBtnClick() {
        //使用代理调用协议方法
    
        //if (代理 != nil)

        vistorViewDelegate?.userWillLogin()
    
    }
    @objc private func registerBtnClick() {
     
        // if([代理 responseSelxxxx])

        vistorViewDelegate?.userWillRegister?()
    
    }
    
    
    
    //外界的接口
    func setupUIWithInfo(imageName: String?,tipText: String) {
    
        if let img = imageName {
        circleIcon.image = UIImage(named: img)
            
        //隐藏大房子
        largeHouse.hidden = true
            bringSubviewToFront(circleIcon)
            
            
           
        }else{
            
        //在首页圆圈有动画效果
         startAnimation()
        
        }
    tipLabel.text = tipText
    
    }
    
    //给首页圆圈设置动画效果
    func startAnimation() {
    
    //社会基本动画
    let basic = CABasicAnimation(keyPath: "transform.rotation")
       
    //设置重复次数
    basic.repeatCount = MAXFLOAT
        
    //设置范围
    basic.toValue = 2 * M_PI
    
    //设置持续时间
    basic.duration = 10
        //动画完成后,不移除图层
      basic.removedOnCompletion =  false  
    circleIcon.layer.addAnimation(basic, forKey: nil)
    
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setupUI() {
        addSubview(circleIcon)
        addSubview(backView)
        addSubview(largeHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //设置约束
        circleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
            
        }
        
        largeHouse.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        
        tipLabel.snp_makeConstraints { (make) -> Void in
           
            make.top.equalTo(circleIcon.snp_bottom).offset(20)
            make.centerX.equalTo(circleIcon.snp_centerX)
            make.width.equalTo(240)
            make.height.equalTo(50)
            
        }
        
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(tipLabel.snp_bottom).offset(20)
            make.left.equalTo(tipLabel.snp_left)
            make.width.equalTo(100)
        }
        
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(tipLabel.snp_bottom).offset(20)
            make.right.equalTo(tipLabel.snp_right)
            make.width.equalTo(100)
            
        }
        
        backView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(registerBtn.snp_bottom)
        }
    self.backgroundColor = UIColor(white: 0.93, alpha: 1.0)
    
        //添加监听事件
        loginBtn.addTarget(self, action: "loginBtnClick", forControlEvents: .TouchUpInside)
        
        registerBtn.addTarget(self, action: "registerBtnClick", forControlEvents: .TouchUpInside)
    
    
    
    }

    
    //6.加载背景
    lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //1.加载圆圈
    lazy var circleIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //2.加载大房子
    lazy var largeHouse: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //3.加载提示文案
//    lazy var tipLabel : UILabel = {
//    
//    let l = UILabel()
 //       l.textColor = UIColor.darkGrayColor()
//        l.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
//        l.font = UIFont.systemFontOfSize(15)
//        l.textAlignment = .Center
//        l.numberOfLines = 0
//        l.sizeToFit()
//        return l
//    
//    }()
    lazy var tipLabel: UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜", fontSize: 15, color: UIColor.darkGrayColor())
//    
//    //4.加载登陆按钮
//    lazy var loginBtn: UIButton = {
//    let btn = UIButton()
//
//        btn.setTitle("登录", forState: .Normal)
//        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
//        btn.titleLabel?.font = UIFont.systemFontOfSize(16)
//        let image = UIImage(named: "common_button_white_disable")
//        let w = Int(image!.size.width/2)
//        let h = Int(image!.size.height/2)
//        btn.setBackgroundImage(image?.stretchableImageWithLeftCapWidth(w, topCapHeight: h), forState: .Normal)
//        
//    return btn
//    
//    }()
//    
//    //5.加载注册按钮
//    lazy var registerBtn: UIButton = {
//    
//    let btn = UIButton()
//    btn.setTitle("注册", forState: .Normal)
//        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
//    btn.titleLabel?.font = UIFont.systemFontOfSize(16)
//        let image = UIImage(named: "common_button_white_disable")
//        let w = Int(image!.size.width/2)
//        let h = Int(image!.size.height/2)
//        btn.setBackgroundImage(image?.stretchableImageWithLeftCapWidth(w, topCapHeight: h), forState: .Normal)
//        return btn
//    }()
    
    
   //4.登录按钮
    lazy var loginBtn: UIButton = UIButton(title: "登录", color: UIColor.darkGrayColor(), fontSize: 16, backImageName: "common_button_white_disable")
    
    //5.注册按钮
    lazy var registerBtn: UIButton = UIButton(title: "注册", color: UIColor.orangeColor(), fontSize: 16, backImageName: "common_button_white_disable")
    

}
